package site.addzero.component.table.biz

import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Sort
import androidx.compose.material.icons.filled.ArrowDownward
import androidx.compose.material.icons.filled.ArrowUpward
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import site.addzero.component.button.AddIconButton
import site.addzero.component.table.original.entity.ColumnConfig
import site.addzero.entity.low_table.EnumSortDirection

@Composable
fun <C> RenderSortButton(
    column: C,
    getColumnKey: (C) -> String,
    columnConfigs: List<ColumnConfig>,
    sortDirection: EnumSortDirection,
    onClick: () -> Unit
) {
    val columnKey = getColumnKey(column)
    val columnConfig = columnConfigs.find { it.key == columnKey }
    val showSort = columnConfig?.showSort ?: true

    if (!showSort) {
        return
    }

    val (text, icon) = when (sortDirection) {
        EnumSortDirection.ASC -> "升序" to Icons.Default.ArrowUpward
        EnumSortDirection.DESC -> "降序" to Icons.Default.ArrowDownward
        else -> "默认" to Icons.AutoMirrored.Filled.Sort
    }

    AddIconButton(
        text = text,
        imageVector = icon,
        tint = MaterialTheme.colorScheme.primary,
        modifier = Modifier.size(16.dp)
    ) {
        onClick()
    }
}
